%Script to run lin_liblinear with different solvers and stores the results
%of the values into a text file

%% Set up libraries

%addpath libsvm
addpath liblinear


%% Should be done in run_submission before calling this script

% Load the data
% load ../data/data_no_bigrams.mat;

% Make the training data
% X = make_sparse(train);
% Y = double([train.rating]');

% Make our own test/training set
% XcvTrain = make_sparse(train(bsxfun(@gt, [train().category], 6)));
% XcvTest = make_sparse(train(bsxfun(@lt, [train().category], 7)));
% 
% YcvTrain = double([train(bsxfun(@gt, [train().category], 6)).rating])';
% YcvTest = double([train(bsxfun(@lt, [train().category], 7)).rating])';

% Used to calculate expected value
ratings = [2 1 5 4];

%% Liblinear SVM

%s = [2 3 4 5 6 7];
s = [7]; %- Already did - see stats_solver_0.txt
for solver = s(1:end)
    
    % Train and test on word count data

    %stats = fopen(sprintf('stats_solver_%d.txt', solver),'a');
    
    % Train on ALL data, test on CV set (there's overlap btw train and test,
    %   may overfit)
    %[results, info, yhat] = lin_liblinear(X, Y, XcvTest, YcvTest, solver);
    % Train on CV data, test on CV set
    B = 0.1;
    [results, info, yhat] = lin_liblinear(XcvTrain, YcvTrain, ...
      XcvTest, YcvTest, solver, B);
    % Calc expected value and RMSE
    [rmse, rmseE, yhatE] = calc_rmse (info.vals, ratings, YcvTest, yhat);

    %fprintf(stats, 'For solver %d \nbest C is: %g \nRMSE is: %g\nExpected RMSE is: %g\n',solver,info.bestC,rmseMini,rmseMiniE);
    %fclose(stats);


    % Train and test on title and helpful rating data

    % Train
    [results_th, info_th, yhat_th] = lin_liblinear( ...
      XcvTrainHelpTitle, YcvTrain, ...
      XcvTestHelpTitle, YcvTest, solver);
    % Calc expected value and RMSE
    [rmse_th, rmseE_th, yhatE_th] = calc_rmse (info_th.vals, ratings, ...
      YcvTest, yhat_th);


    % Combine expected value of the two methods

    yhat_comb = (yhat + yhat_th) ./ 2;
    yhatE_comb = (yhatE + yhatE_th) ./ 2;

    % Calc RMSE for direct predictions and expected values
    n = numel (YcvTest);
    rmse_comb = sqrt (sum ((YcvTest - yhat_comb) .^ 2) / n);
    rmseE_comb = sqrt (sum ((YcvTest - yhatE_comb) .^ 2) / n);
    disp (sprintf ('RMSE %g, RMSE for expected values %g\n', ...
      rmse_comb, rmseE_comb));

end

